/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJPanel.java
 *
 * Created on Jun 13, 2009, 2:17:40 PM
 */

package com.meraklisina.latexeditor.toolbarCustomize ;

import java.util.ArrayList ;

import javax.swing.DefaultListModel ;
import javax.swing.JDialog ;
import javax.swing.JList ;

import com.meraklisina.latexeditor.LatexToolBar ;
import com.meraklisina.latexeditor.ToolBarHandler ;

/**
 *
 * @author Administrator
 */
public class CustomizeForm extends javax.swing.JPanel
{
    /** Creates new form NewJPanel */
    JDialog topContainer = null ;

    public CustomizeForm( JDialog dialog )
    {
        this.topContainer = dialog ;
        this.initComponents( ) ;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings( "unchecked" )
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents( )
    {

        this.jScrollPane1 = new javax.swing.JScrollPane( ) ;
        this.availableList = new javax.swing.JList( ) ;
        this.jScrollPane2 = new javax.swing.JScrollPane( ) ;
        this.visibleList = new javax.swing.JList( ) ;
        this.jButton1 = new javax.swing.JButton( ) ;
        this.jButton2 = new javax.swing.JButton( ) ;
        this.jButton3 = new javax.swing.JButton( ) ;
        this.jButton4 = new javax.swing.JButton( ) ;
        this.jLabel1 = new javax.swing.JLabel( ) ;
        this.jLabel2 = new javax.swing.JLabel( ) ;
        this.jButton5 = new javax.swing.JButton( ) ;
        this.jButton6 = new javax.swing.JButton( ) ;
        this.jButton7 = new javax.swing.JButton( ) ;

        this.availableList.setModel( new javax.swing.AbstractListModel( )
        {
            String[ ] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" } ;

            public int getSize( )
            {
                return this.strings.length ;
            }

            public Object getElementAt( int i )
            {
                return this.strings[ i ] ;
            }
        } ) ;
        this.jScrollPane1.setViewportView( this.availableList ) ;

        this.visibleList.setModel( new javax.swing.AbstractListModel( )
        {
            String[ ] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" } ;

            public int getSize( )
            {
                return this.strings.length ;
            }

            public Object getElementAt( int i )
            {
                return this.strings[ i ] ;
            }
        } ) ;
        this.jScrollPane2.setViewportView( this.visibleList ) ;

        this.jButton1.setText( "<" ) ;
        this.jButton1.addActionListener( new java.awt.event.ActionListener( )
        {
            public void actionPerformed( java.awt.event.ActionEvent evt )
            {
                CustomizeForm.this.addOneToLeft( evt ) ;
            }
        } ) ;

        this.jButton2.setText( ">" ) ;
        this.jButton2.addActionListener( new java.awt.event.ActionListener( )
        {
            public void actionPerformed( java.awt.event.ActionEvent evt )
            {
                CustomizeForm.this.addOneToRight( evt ) ;
            }
        } ) ;

        this.jButton3.setText( "<<" ) ;
        this.jButton3.addActionListener( new java.awt.event.ActionListener( )
        {
            public void actionPerformed( java.awt.event.ActionEvent evt )
            {
                CustomizeForm.this.addAllToLeft( evt ) ;
            }
        } ) ;

        this.jButton4.setText( ">>" ) ;
        this.jButton4.addActionListener( new java.awt.event.ActionListener( )
        {
            public void actionPerformed( java.awt.event.ActionEvent evt )
            {
                CustomizeForm.this.addAllToRight( evt ) ;
            }
        } ) ;

        this.jLabel1.setText( "Available" ) ;

        this.jLabel2.setText( "Visible" ) ;

        this.jButton5.setText( "Close" ) ;
        this.jButton5.addActionListener( new java.awt.event.ActionListener( )
        {
            public void actionPerformed( java.awt.event.ActionEvent evt )
            {
                CustomizeForm.this.jButton5ActionPerformed( evt ) ;
            }
        } ) ;

        this.jButton6.setText( "Up" ) ;
        this.jButton6.addActionListener( new java.awt.event.ActionListener( )
        {
            public void actionPerformed( java.awt.event.ActionEvent evt )
            {
                CustomizeForm.this.upButtonPressed( evt ) ;
            }
        } ) ;

        this.jButton7.setText( "Down" ) ;
        this.jButton7.addActionListener( new java.awt.event.ActionListener( )
        {
            public void actionPerformed( java.awt.event.ActionEvent evt )
            {
                CustomizeForm.this.downButtonPressed( evt ) ;
            }
        } ) ;

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout( this ) ;
        this.setLayout( layout ) ;
        layout.setHorizontalGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING ).addGroup( layout.createSequentialGroup( ).addContainerGap( ).addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING, false ).addComponent( this.jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE ).addComponent( this.jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE ) ).addGap( 36, 36, 36 ).addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING, false ).addComponent( this.jButton3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE ).addComponent( this.jButton4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE ).addComponent( this.jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE ).addComponent( this.jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE ) ).addGap( 29, 29, 29 ).addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING, false ).addComponent( this.jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE ).addComponent( this.jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE ) ).addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED ).addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING, false ).addComponent( this.jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE ).addComponent( this.jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE ) ).addContainerGap( 24, Short.MAX_VALUE ) ).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup( ).addContainerGap( 344, Short.MAX_VALUE ).addComponent( this.jButton5 ).addContainerGap( ) ) ) ;
        layout.setVerticalGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING ).addGroup( layout.createSequentialGroup( ).addGap( 13, 13, 13 ).addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE ).addComponent( this.jLabel1 ).addComponent( this.jLabel2 ) ).addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED ).addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING ).addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING, false ).addGroup( layout.createSequentialGroup( ).addComponent( this.jButton3 ).addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED ).addComponent( this.jButton1 ).addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED ).addComponent( this.jButton2 ).addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED ).addComponent( this.jButton4 ) ).addComponent( this.jScrollPane1, 0, 0, Short.MAX_VALUE ).addComponent( this.jScrollPane2, 0, 0, Short.MAX_VALUE ) ).addGroup( layout.createSequentialGroup( ).addComponent( this.jButton6 ).addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED ).addComponent( this.jButton7 ) ) ).addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED, 118, Short.MAX_VALUE ).addComponent( this.jButton5 ).addContainerGap( ) ) ) ;
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Since this ui holds two lists and switch between them are the most frequent use case,
     * @param source Source JList that item will be taken
     * @param destination Destination JList that item will be sent
     * @param index Index of item to be switched
     */
    private void switchObject( JList source, JList destination, int index )
    {
        LatexToolBar selectedValue = ( LatexToolBar )( ( DefaultListModel )source.getModel( ) ).getElementAt( index ) ;
        ( ( DefaultListModel )source.getModel( ) ).remove( index ) ;
        DefaultListModel dlm = ( DefaultListModel )destination.getModel( ) ;
        dlm.addElement( selectedValue ) ;
    }

    /**
     * Takes selected item from left jlist and put to right jlist, UI updates also take place here.
     * @param evt Event
     */
    private void addOneToRight( java.awt.event.ActionEvent evt )
    {//GEN-FIRST:event_addOneToRight
        Object selectedValue = this.availableList.getSelectedValue( ) ;
        if( selectedValue instanceof LatexToolBar )
        {
            LatexToolBar ltb = ( LatexToolBar )selectedValue ;
            ToolBarHandler.getHandler( ).AddToolBarToPanel( ltb ) ;
            this.switchObject( this.availableList, this.visibleList, this.availableList.getSelectedIndex( ) ) ;
        }
    }//GEN-LAST:event_addOneToRight

    /**
     * Takes all items from left jlist and put to right jlist, UI updates also take place here.
     * @param evt
     */
    private void addAllToRight( java.awt.event.ActionEvent evt )
    {//GEN-FIRST:event_addAllToRight
        DefaultListModel dlm = ( DefaultListModel )this.availableList.getModel( ) ;
        int size = dlm.getSize( ) ;
        for( int i = 0; i < size; i++ )
        {
            LatexToolBar ltb = ( LatexToolBar )dlm.getElementAt( 0 ) ;
            ToolBarHandler.getHandler( ).AddToolBarToPanel( ltb ) ;
            this.switchObject( this.availableList, this.visibleList, 0 ) ;

        }
    }//GEN-LAST:event_addAllToRight

    /**
     * Takes selected item from right jlist and put to left jlist, UI updates also take place here.
     * @param evt
     */
    private void addOneToLeft( java.awt.event.ActionEvent evt )
    {//GEN-FIRST:event_addOneToLeft
        Object selectedValue = this.visibleList.getSelectedValue( ) ;
        if( selectedValue instanceof LatexToolBar )
        {
            LatexToolBar ltb = ( LatexToolBar )selectedValue ;
            ToolBarHandler.getHandler( ).RemoveToolBarFromPanel( ltb ) ;
            this.switchObject( this.visibleList, this.availableList, this.visibleList.getSelectedIndex( ) ) ;
        }
    }//GEN-LAST:event_addOneToLeft

    /**
     * Takes all items from right jlist and put to left jlist, UI updates also take place here.
     * @param evt
     */
    private void addAllToLeft( java.awt.event.ActionEvent evt )
    {//GEN-FIRST:event_addAllToLeft
        DefaultListModel dlm = ( DefaultListModel )this.visibleList.getModel( ) ;
        int size = dlm.getSize( ) ;
        for( int i = 0; i < size; i++ )
        {
            LatexToolBar ltb = ( LatexToolBar )dlm.getElementAt( 0 ) ;
            ToolBarHandler.getHandler( ).RemoveToolBarFromPanel( ltb ) ;
            this.switchObject( this.visibleList, this.availableList, 0 ) ;
        }
    }//GEN-LAST:event_addAllToLeft

    private void jButton5ActionPerformed( java.awt.event.ActionEvent evt )
    {//GEN-FIRST:event_jButton5ActionPerformed
        this.topContainer.setVisible( false ) ;
    }//GEN-LAST:event_jButton5ActionPerformed

    /**
     * This method increases order of selected toolbar.
     * @param evt
     */
    private void upButtonPressed( java.awt.event.ActionEvent evt )
    {//GEN-FIRST:event_upButtonPressed
        DefaultListModel dlm = ( DefaultListModel )this.visibleList.getModel( ) ;
        int selectedIndex = this.visibleList.getSelectedIndex( ) ;
        if( selectedIndex > 0 )
        {
            LatexToolBar selected = ( LatexToolBar )dlm.getElementAt( selectedIndex ) ;
            LatexToolBar upper = ( LatexToolBar )dlm.getElementAt( selectedIndex - 1 ) ;
            // swap orders
            int upperOrder = upper.getOrder( ) ;
            upper.setOrder( selected.getOrder( ) ) ;
            selected.setOrder( upperOrder ) ;
            dlm.removeElementAt( selectedIndex ) ;
            dlm.insertElementAt( selected, selectedIndex - 1 ) ;
            this.visibleList.setSelectedIndex( selectedIndex - 1 ) ;
        }
        ToolBarHandler.sort( ToolBarHandler.getHandler( ).getToolbars( ) ) ;
        ToolBarHandler.getHandler( ).refreshVisuals( ) ;
    }//GEN-LAST:event_upButtonPressed

    /**
     * This method decreases order of selected toolbar.
     * @param evt
     */
    private void downButtonPressed( java.awt.event.ActionEvent evt )
    {//GEN-FIRST:event_downButtonPressed
        DefaultListModel dlm = ( DefaultListModel )this.visibleList.getModel( ) ;
        int selectedIndex = this.visibleList.getSelectedIndex( ) ;
        if( ( selectedIndex > -1 ) && ( selectedIndex < dlm.size( ) - 1 ) )
        {
            LatexToolBar selected = ( LatexToolBar )dlm.getElementAt( selectedIndex ) ;
            LatexToolBar lower = ( LatexToolBar )dlm.getElementAt( selectedIndex + 1 ) ;
            // swap orders
            int lowerOrder = lower.getOrder( ) ;
            lower.setOrder( selected.getOrder( ) ) ;
            selected.setOrder( lowerOrder ) ;
            dlm.removeElementAt( selectedIndex ) ;
            dlm.insertElementAt( selected, selectedIndex + 1 ) ;
            this.visibleList.setSelectedIndex( selectedIndex + 1 ) ;
        }
        ToolBarHandler.sort( ToolBarHandler.getHandler( ).getToolbars( ) ) ;
        ToolBarHandler.getHandler( ).refreshVisuals( ) ;
    }//GEN-LAST:event_downButtonPressed

    /**
     * This method refills both JLists, based on current toolbar configuration
     */
    public void reload( )
    {
        ArrayList<LatexToolBar> toolbars = ToolBarHandler.getHandler( ).getToolbars( ) ;
        ArrayList<LatexToolBar> notUsed = new ArrayList<LatexToolBar>( ) ;
        ArrayList<LatexToolBar> used = new ArrayList<LatexToolBar>( ) ;
        for( LatexToolBar tb : toolbars )
        {
            if( tb.isAvailable( ) )
            {
                used.add( tb ) ;
            }
            else
            {
                notUsed.add( tb ) ;
            }
        }
        DefaultListModel dlm = new DefaultListModel( ) ;
        for( LatexToolBar tb : notUsed )
        {
            dlm.addElement( tb ) ;
        }
        this.availableList.setModel( dlm ) ;
        dlm = new DefaultListModel( ) ;
        for( LatexToolBar tb : used )
        {
            dlm.addElement( tb ) ;
        }
        this.visibleList.setModel( dlm ) ;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList availableList ;

    private javax.swing.JButton jButton1 ;

    private javax.swing.JButton jButton2 ;

    private javax.swing.JButton jButton3 ;

    private javax.swing.JButton jButton4 ;

    private javax.swing.JButton jButton5 ;

    private javax.swing.JButton jButton6 ;

    private javax.swing.JButton jButton7 ;

    private javax.swing.JLabel jLabel1 ;

    private javax.swing.JLabel jLabel2 ;

    private javax.swing.JScrollPane jScrollPane1 ;

    private javax.swing.JScrollPane jScrollPane2 ;

    private javax.swing.JList visibleList ;
    // End of variables declaration//GEN-END:variables

}
